clear all
use  "../data/zip_year_data", clear

*Define CHTV and CHTC and the IV 
gen lnchtc = log((chinese100cash - american100cash/american100*chinese100)*(american100/(american100 - american100cash)))
gen chtc = (chinese100cash - american100cash/american100*chinese100)*(american100/(american100 - american100cash))

gen lnchtv = log((chinese100_cashval - american100_cashval/american100_val*chinese100_val)*(american100_val/(american100_val - american100_cashval)))
gen chtv = (chinese100_cashval - american100_cashval/american100_val*chinese100_val)*(american100_val/(american100_val - american100_cashval))

by year, sort: egen chttc = sum(chtc)
gen chi_iv_ct = log(chineseper * chttc)
by year, sort: egen chttv = sum(chtv)
gen chi_iv_val = log(chineseper * chttv)

*Define the interacted terms
gen lnchtvXpost = lnchtv * post
gen lnchtcXpost = lnchtc * post
gen chi_iv_ctXpost = chi_iv_ct * post
gen chi_iv_valXpost = chi_iv_val * post

*Keep only common sample where both lnchtv and lnchtc are not missing
drop if lnchtc == .
drop if lnchtv == .

*emp
reghdfe lnchtv lnpopulation d_lnemp_9600 bachelorshare d_lnincome_9801 ln_density near_college_5 chi_iv_val chi_iv_valXpost if chtv != 0, absorb(timecounty) cluster(zipcode) 
eststo r1
estadd local c "Yes"
estadd local cyfe "Yes"
estadd scalar fstat = e(widstat)

reghdfe lnchtc lnpopulation d_lnemp_9600 bachelorshare d_lnincome_9801 ln_density near_college_5 chi_iv_ct chi_iv_ctXpost if chtv != 0, absorb(timecounty) cluster(zipcode) 
eststo r2
estadd local c "Yes"
estadd local cyfe "Yes"
estadd scalar fstat = e(widstat)

reghdfe lnchtvXpost lnpopulation d_lnemp_9600 bachelorshare d_lnincome_9801 ln_density near_college_5 chi_iv_val chi_iv_valXpost if chtv != 0, absorb(timecounty) cluster(zipcode) 
eststo r3
estadd local c "Yes"
estadd local cyfe "Yes"
estadd scalar fstat = e(widstat)

reghdfe lnchtcXpost lnpopulation d_lnemp_9600 bachelorshare d_lnincome_9801 ln_density near_college_5 chi_iv_ct chi_iv_ctXpost if chtv != 0, absorb(timecounty) cluster(zipcode) 
eststo r4
estadd local c "Yes"
estadd local cyfe "Yes"
estadd scalar fstat = e(widstat)


esttab r1 r2 r3 r4  ///
	using "../results/tableA1_a.tex", replace ///
	label ///
	order(chi_iv_val chi_iv_valXpost chi_iv_ct chi_iv_ctXpost) ///
	keep(chi_iv_val chi_iv_valXpost chi_iv_ct chi_iv_ctXpost) ///
	star(* 0.10 ** 0.05 *** 0.01) ///
	cells(b(star fmt(%9.3f)) se(par fmt(%9.3f))) ///
	stats(c cyfe N , ///
		fmt(0 0 %11.0g ) ///
		labels("Controls" ///
			"County-year FE" ///
			"Observations")) ///
	 mlabels(, none) collabels(, none) ///
	 mgroups("ln(CHTV)" "ln(CHTC)" "ln(CHTV)*Post" "ln(CHTC)*Post", pattern(1 1 1 1 ))
 
*zillow
reghdfe lnchtv lnpopulation d_lnzhvi_real_9600 bachelorshare d_lnincome_9801 ln_density near_college_5 chi_iv_val chi_iv_valXpost if chtv != 0, absorb(timecounty) cluster(zipcode) 
eststo r1
estadd local c "Yes"
estadd local cyfe "Yes"
estadd scalar fstat = e(widstat)

reghdfe lnchtc lnpopulation d_lnzhvi_real_9600 bachelorshare d_lnincome_9801 ln_density near_college_5 chi_iv_ct chi_iv_ctXpost if chtv != 0, absorb(timecounty) cluster(zipcode) 
eststo r2
estadd local c "Yes"
estadd local cyfe "Yes"
estadd scalar fstat = e(widstat)

reghdfe lnchtvXpost lnpopulation d_lnzhvi_real_9600 bachelorshare d_lnincome_9801 ln_density near_college_5 chi_iv_val chi_iv_valXpost if chtv != 0, absorb(timecounty) cluster(zipcode) 
eststo r3
estadd local c "Yes"
estadd local cyfe "Yes"
estadd scalar fstat = e(widstat)

reghdfe lnchtcXpost lnpopulation d_lnzhvi_real_9600 bachelorshare d_lnincome_9801 ln_density near_college_5 chi_iv_ct chi_iv_ctXpost if chtv != 0, absorb(timecounty) cluster(zipcode) 
eststo r4
estadd local c "Yes"
estadd local cyfe "Yes"
estadd scalar fstat = e(widstat)


esttab r1 r2 r3 r4  ///
	using "../results/tableA1_b.tex", replace ///
	label ///
	order(chi_iv_val chi_iv_valXpost chi_iv_ct chi_iv_ctXpost) ///
	keep(chi_iv_val chi_iv_valXpost chi_iv_ct chi_iv_ctXpost) ///
	star(* 0.10 ** 0.05 *** 0.01) ///
	cells(b(star fmt(%9.3f)) se(par fmt(%9.3f))) ///
	stats(c cyfe N , ///
		fmt(0 0 %11.0g ) ///
		labels("Controls" ///
			"County-year FE" ///
			"Observations")) ///
	 mlabels(, none) collabels(, none) ///
	 mgroups("ln(CHTV)" "ln(CHTC)" "ln(CHTV)*Post" "ln(CHTC)*Post", pattern(1 1 1 1 ))
 
